package ch.fusun.baron.player.server.ui;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import ch.fusun.baron.coatofarms.ui.CoatOfArmsViewer;
import ch.fusun.baron.player.Dynasty;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	/** The plug-in ID */
	public static final String PLUGIN_ID = "ch.fusun.baron.player.server.ui"; //$NON-NLS-1$

	/**
	 * <img src="../../../../../../../icons/man.png">
	 */
	public static final String MAN = "icons/man.png"; //$NON-NLS-1$

	/**
	 * <img src="../../../../../../../icons/woman.png">
	 */
	public static final String WOMAN = "icons/woman.png"; //$NON-NLS-1$

	/**
	 * <img src="../../../../../../../icons/boy.png">
	 */
	public static final String BOY = "icons/boy.png"; //$NON-NLS-1$

	/**
	 * <img src="../../../../../../../icons/girl.png">
	 */
	public static final String GIRL = "icons/girl.png"; //$NON-NLS-1$

	/**
	 * <img src="../../../../../../../icons/baby.png">
	 */
	public static final String BABY = "icons/baby.png"; //$NON-NLS-1$

	private static final String ICONS_EMPTY = "icons/empty.png"; //$NON-NLS-1$

	// The shared instance
	private static Activator plugin;

	/**
	 * The constructor
	 */
	public Activator() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
	 * )
	 */
	@Override
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
	 * )
	 */
	@Override
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns an image descriptor for the image file at the given plug-in
	 * relative path
	 * 
	 * @param path
	 *            the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}

	/**
	 * @param path
	 *            the path to the image
	 * @return The image
	 */
	public static Image getImage(String path) {
		Image image = getDefault().getImageRegistry().get(path);
		if (image == null) {
			image = getImageDescriptor(path).createImage();
			getDefault().getImageRegistry().put(path, image);
		}
		return image;
	}

	/**
	 * @param dynasty
	 *            The dynasty to draw
	 * @return The rendered image
	 */
	public static Image getImageForDynasty(Dynasty dynasty) {
		Image image = getDefault().getImageRegistry().get(dynasty.getName());
		if (image == null) {
			Image empty = Activator.getImage(ICONS_EMPTY);
			image = new Image(empty.getDevice(), empty, SWT.IMAGE_COPY);
			CoatOfArmsViewer.drawOnGC(dynasty.getCoatOfArms(), image, 32, 32,
					64, 64);
			getDefault().getImageRegistry().put(dynasty.getName(), image);
		}

		return image;
	}

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

}
